﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MethodWorx.CMS.UI.Models;
using MethodWorx.CMS.Services;
using MethodWorx.CMS.UI.Areas.Administration.Models;

namespace MethodWorx.CMS.UI.Areas.Administration.Controllers
{
    public class ExportController : Controller
    {
        private SchemaService schema;
        private ContentService content;

        //
        // GET: /Export/
        public ExportController(SchemaService schema, ContentService content)
        {
            this.schema = schema;
            this.content = content;
        }

        [HttpGet]
        public ActionResult Index()
        {
            ExportModel model = new ExportModel();

            List<ObjectForExport> objects = new List<ObjectForExport>();

            objects.AddRange(this.GetPageTemplatesForExport());
            objects.AddRange(this.GetContentTemplatesForExport());
            objects.AddRange(this.GetPagesForExport());

            model.Objects = objects;

            return View(model);
        }

        [HttpPost]
        public ActionResult Index(string submit)
        {
            ExportModel model = new ExportModel();

            List<ObjectForExport> objects = new List<ObjectForExport>();

            objects.AddRange(this.GetPageTemplatesForExport());
            objects.AddRange(this.GetContentTemplatesForExport());
            objects.AddRange(this.GetPagesForExport());

            model.Objects = objects;

            this.UpdateModel(model);

            return View(model);
        }

        private IEnumerable<ObjectForExport> GetPageTemplatesForExport()
        {
            foreach (var pt in this.schema.GetAllPageTemplates())
            {
                yield return new ObjectForExport()
                {
                    Id = pt.Id,
                    Type = "PageTemplate",
                    Path = pt.Name,
                    Name = pt.Name,
                    ImportBehavior = "Force Overwrite"
                };
            }
        }

        private IEnumerable<ObjectForExport> GetContentTemplatesForExport()
        {
            foreach (var pt in this.schema.GetAllContentTemplates())
            {
                yield return new ObjectForExport()
                {
                    Id = pt.Id,
                    Type = "ContentTemplate",
                    Path = pt.Name,
                    Name = pt.Name,
                    ImportBehavior = "Force Overwrite"
                };
            }
        }

        private IEnumerable<ObjectForExport> GetPagesForExport()
        {
            foreach (var pt in this.content.GetAllPages())
            {
                yield return new ObjectForExport()
                {
                    Id = pt.Id,
                    Type = "Page",
                    Path = pt.Path,
                    Name = pt.Name,
                    ImportBehavior = "Force Overwrite"
                };
            }
        }

    }
}
